package com.six.firstpage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six.core.entity.Goods;
import com.six.core.entity.Zone;
import com.six.firstpage.mapper.ZoneGoodsMapper;
import com.six.firstpage.service.ZoneService;
import com.six.firstpage.mapper.ZoneMapper;
import com.six.core.vo.IZoneGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author ZZH
* @description 针对表【tb_zone(专区)】的数据库操作Service实现
* @createDate 2023-02-27 10:21:18
*/
@Service
public class ZoneServiceImpl extends ServiceImpl<ZoneMapper, Zone>
    implements ZoneService{

    @Autowired
    private  ZoneMapper zoneMapper;

    @Autowired
    private ZoneGoodsMapper zoneGoodsMapper;


    @Override
    public List<Zone> getAllZone() {
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("is_disable",0);
        List<Zone> zones = zoneMapper.selectList(wrapper);

        List<IZoneGoodsVo> allZoneGoods = zoneGoodsMapper.getAllZoneGoods();

        zones.stream().forEach(
                zone -> {
                    ArrayList<IZoneGoodsVo> vos = new ArrayList<>();
                    allZoneGoods.stream().forEach(zoneGoods->{
                        if (zone.getId().equals(zoneGoods.getZid())){
                            vos.add(zoneGoods);
                        }
                    });
                    zone.setIZoneGoodsVo(vos);
                }
        );

        return zones;
    }

    //查询专区
    @Override
    public Page<Zone> getZoneByName(String name, Integer status, Integer current, Integer pageSize) {
        Page<Zone> zonePage = new Page<>(current, pageSize);
        QueryWrapper<Zone> wrapper = new QueryWrapper<>();
        if (name!=null){
            wrapper.like("zone_name",name);
        }
        if (status!=null){
            wrapper.eq("is_disable", status);
        }
        wrapper.eq("is_deleted",0);
        Page<Zone> zonePage1 = zoneMapper.selectPage(zonePage, wrapper);

        return zonePage1;
    }

    //添加专区
    @Override
    public Integer addZone(Zone zone) {
        return zoneMapper.insert(zone);
    }

    //修改专区
    @Override
    public Integer updateZone(Zone zone) {
        return zoneMapper.updateById(zone);
    }

    //逻辑删除专区
    @Override
    public Integer delZone(Integer id) {
        UpdateWrapper<Zone> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("is_deleted",1);
        return zoneMapper.update(null,wrapper);
    }

    //根据专区id查询商品
    @Override
    public Page<Goods> getGooodsByZoneId(Integer zoneId) {
        return null;
    }
}




